/******************************************************************************
 * @File name   :      JobMgntController.java
 *
 * @Author      :      cai junyi
 *
 * @Date        :      2011-10-25
 *
 * @Copyright Notice: 
 * Copyright (c) 2014 DMS, Inc. All  Rights Reserved.
 * This software is published under the terms of the DMS Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2011-10-25 上午9:38:26        cai junyi     1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.framework.common.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.cap.bts.framework.bizlog.service.IBizLoggerService;
import com.cap.bts.framework.common.constant.CommonConstants;
import com.cap.bts.framework.common.exception.AppException;
import com.cap.bts.framework.common.service.IJobMgntService;
import com.cap.bts.framework.common.util.CodeTextUtils;
import com.cap.bts.framework.scheduler.dto.JobQueryDto;
import com.cap.bts.framework.scheduler.dto.JobResultDto;

/**
 * 
 */
@Controller
@RequestMapping(value = "/common/job")
public class JobMgntController extends BaseController{
    
	private static Logger logger = Logger.getLogger(JobMgntController.class);
	

    @Autowired
	protected IBizLoggerService bizLoggerService;
    
    @Autowired
    private IJobMgntService jobMgntService;
    
    private void initStaticData(Model model){
        Map<String, String> typeMap = CodeTextUtils.getStatusMap("Job.type");
        Map<String, String> statusMap = CodeTextUtils.getStatusMap("Job.status");
        model.addAttribute("typeMap", typeMap);
        model.addAttribute("statusMap", statusMap);
    }
    
    /**
     * 查询
     * @Date        :      2011-12-7
     * @param queryJobDto
     * @param model
     * @return
     * @throws AppException
     */
    @RequestMapping(value = "/getJobs")
    public String getJobsByDto(JobQueryDto queryJobDto,Model model) throws AppException{
        initStaticData(model);
        List<JobResultDto> jobs = jobMgntService.getJobByQueryDto(queryJobDto);
        for (Iterator<JobResultDto> iterator = jobs.iterator(); iterator.hasNext();) {//找出需要重做的job
            JobResultDto jobResultDto = (JobResultDto) iterator.next();
            if(jobResultDto.getJobStatus()!=1){
//                needRedoJobs.add(jobResultDto);
                
            }
        }
        model.addAttribute("jobs", jobs);
        model.addAttribute("queryJobDto", queryJobDto);
        model.addAttribute(CommonConstants.REQUEST_ATTRIBUTE_PAGER, queryJobDto.getPager());
        
        return "common/job_mgnt/search_job_mgnt";
    }
    /**
     * 重试
     * @Date        :      2011-12-7
     * @param jobName
     * @param model
     * @return
     * @throws AppException
     */
    @RequestMapping(value = "/retryJob/{jobName}", method = RequestMethod.GET)
    public String retryJob(@PathVariable("jobName") String jobName,Model model) throws AppException,Exception{
        JobQueryDto queryJobDto = new JobQueryDto();
        try{
            jobMgntService.retryJob(jobName);
        }catch(Exception e){
            throw e;
        }
                //go to search page
        return getJobsByDto(queryJobDto,model);
    }
    /**
     * 查询
     * @Date        :      2011-12-7
     * @param queryJobDto
     * @param model
     * @return
     * @throws AppException
     */
    @RequestMapping(value = "/getJobsForMonitor")
    public String getJobsForMonitor(JobQueryDto queryJobDto,Model model) throws AppException{
        initStaticData(model);
        List<JobResultDto> jobs = jobMgntService.getJobByQueryDto(queryJobDto);
        List<JobResultDto> needRedoJobs =new ArrayList<JobResultDto>();
        for (Iterator<JobResultDto> iterator = jobs.iterator(); iterator.hasNext();) {//找出需要重做的job
            JobResultDto jobResultDto = (JobResultDto) iterator.next();
            if(jobResultDto.getJobStatus()==2 || jobResultDto.getJobStatus()==3){
                
            }
        }
        boolean needAlarm=false;
        if(needRedoJobs!=null && needRedoJobs.size()>0){
            needAlarm=true;
        }
        model.addAttribute("needAlert", needAlarm);
        
        model.addAttribute("jobs", needRedoJobs);
        model.addAttribute("queryJobDto", queryJobDto);
       
        model.addAttribute(CommonConstants.REQUEST_ATTRIBUTE_PAGER, queryJobDto.getPager());
        
        return "common/job_mgnt/search_job_mgnt_for_monitor";
    }
    
    /**
     * 根据任务名称重计定时任务运行记录
     * @Date        :      Dec 6, 2011
     * @param jobName
     * @param model
     * @return
     * @throws AppException
     */
    @RequestMapping(value = "/retryJobForMoniotr/{jobName}", method = RequestMethod.GET)
    public String retryJobForMoniotr(@PathVariable("jobName") String jobName,Model model) 
    throws AppException,Exception{
        JobQueryDto queryJobDto = new JobQueryDto();
        
        try{
            jobMgntService.retryJob(jobName);
        }catch(Exception e){
            throw e;
        }
        //go to search page
//        JobQueryDto queryJobDto = new JobQueryDto();
        return getJobsForMonitor(queryJobDto,model);
    }

    


}
